//
//  SelectMenuCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/30.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "SelectMenuCell.h"

@implementation SelectMenuCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.countTF.delegate = self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.mainImageView pp_clipCorners:UIRectCornerAllCorners radius:6];
    });
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    if (self.textFieldShouldEndEditingBlock) {
        self.textFieldShouldEndEditingBlock(textField);
    }
    return YES;
}

- (void)fillCellWithModel:(CommonMenuModel *)model {
    
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.bhmTransverseImg] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    self.mainTitleLab.text = model.bhmMenuName;
    self.subTitleLab.text = model.bhmRemark;
    self.priceLab.text = [NSString stringWithFormat:@"¥%@/桌",[Utilities changeDoubleToString:model.bhmMenuPrice]];

    if (model.bhmMenuCount >=0) {
        self.countTF.text = [NSString stringWithFormat:@"%ld",(long)model.bhmMenuCount];
    }
    
    [self.selBtn setImage:[UIImage imageNamed:model.isSelect?@"common_sel":@"common_nor"] forState:UIControlStateNormal];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
    return [self validateNumber:string];
}
 
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
         NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
         NSRange range = [string rangeOfCharacterFromSet:tmpSet];
         if (range.length == 0) {
            res = NO;
            break;
          }
          i++;
       }
    return res;
}


- (IBAction)selAction:(id)sender {
    
    if (self.SelectMenuCellBlock) {
        self.SelectMenuCellBlock(SelectMenuCellActionType_sel);
    }
}

- (IBAction)addAction:(id)sender {
    if (self.SelectMenuCellBlock) {
        self.SelectMenuCellBlock(SelectMenuCellActionType_add);
    }
}

- (IBAction)cutAction:(id)sender {
    if (self.SelectMenuCellBlock) {
        self.SelectMenuCellBlock(SelectMenuCellActionType_cut);
    }
}

@end
